﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModelXML;
using Microsoft.Xna.Framework;

namespace Etapa1.Import.Anim
{

    public class AnimationClip
    {
        internal AnimationClip(){}
        public Dictionary<String, Keyframes[]> boneNameToAnimationClip = new Dictionary<String, Keyframes[]>();        
        //int currentFrame;
        //public void Play()
        //{
        //}
        //public void Stop()
        //{
        //}
        //public void Pause()
        //{
        //}

    }

    public struct Keyframes
    {
        public String boneName;
        public int time;
        public Matrix trans;
    }

    public class Animations
    {
        private Animations() { }
        
        public Dictionary<String, AnimationClip> animations = new Dictionary<string, AnimationClip>();
        
        public static Animations LoadAnimations(Modelo modelo)
        {               
            Animations anim = new Animations();
            foreach (var item in modelo.SkinInfo.AnimationInfos)
            {
                List<Keyframes> specificAnimation = new List<Keyframes>();
                String name = item.AnimationName;
                item.AnimationClips.Sort();
                int animationSize = item.AnimationClips.Last().time;

                foreach (var clip in item.AnimationClips)
                {
                    String boneName = clip.BoneName;
                    int time = clip.time;
                    Matrix trans = ImporterHelper.CreateTransformation(clip.Transformation);                                        
                    Keyframes key = new Keyframes();
                    key.boneName = boneName;
                    key.time = time;
                    key.trans = trans;
                    specificAnimation.Add(key);                    
                }                

                AnimationClip ac = new AnimationClip();
                ac.boneNameToAnimationClip = ProcessAnimationKeyfranes(animationSize, modelo, specificAnimation);
                anim.animations[name] = ac;
            }
            return anim;
        }

        static Dictionary<String, Keyframes[]> ProcessAnimationKeyfranes(int animationTime, Modelo modelo, List<Keyframes> keys)
        {
            Dictionary<String, Keyframes[]> boneNameToAnimationClip = new Dictionary<String, Keyframes[]>();

            foreach (var item in modelo.SkinInfo.Bones)
            {                
                List<Keyframes> frames = keys.FindAll((a) => a.boneName == item.BoneName);
                frames.Sort(new comparer());
                Keyframes[] keyFrames = frames.ToArray();
                boneNameToAnimationClip[item.BoneName] = keyFrames;                
            }
            return boneNameToAnimationClip;
        }

        class comparer : IComparer<Keyframes>
        {
            #region IComparer<Keyframes> Members

            public int Compare(Keyframes x, Keyframes y)
            {
                return x.time.CompareTo(y.time);
            }

            #endregion
        }

    }
}
